package com.ffcs.crmd.tsp.admin.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import com.ctg.itrdc.platform.common.entity.PageInfo;
import com.ctg.itrdc.platform.common.utils.bean.BeanUtils;
import com.ffcs.crmd.tsp.admin.entity.Job;
import com.ffcs.crmd.tsp.admin.service.IJobService;
import com.ffcs.crmd.tsp.api.dto.client.TspJob;
import com.ffcs.crmd.tsp.api.dto.client.TspJobDetail;
import com.ffcs.crmd.tsp.api.dto.client.TspResult;
import com.ffcs.crmd.tsp.client.core.TspClient;
import com.ffcs.crmd.tsp.common.exception.TspException;

/**
 * 
 * 功能说明:
 *
 * @author FFCS-ZHONGFUHUA
 * 
 * @since 
 *
 */
@Service
public class JobServiceImpl extends AbstractService implements IJobService {
    
    
    @Override
    public void deleteJob(Job job) throws TspException {
        try {
            TspJob tspJobVO = new TspJob();
            tspJobVO.setClusterName(job.getClusterName());
            tspJobVO.setId(job.getId());
            
            TspClient tspClient = getClient(job.getClusterName());
            TspResult<?> tspResult = tspClient.deleteJob(tspJobVO);
            validateResult(tspResult);
        } catch (Exception e) {
            throw new TspException(e.getMessage());
        }
    }
    
    
    @Override
    public void deleteJob(List<Job> jobList) throws TspException {
        for(Job job : jobList){
            deleteJob(job);
        }
    }
    
    /**
     * 功能说明:
     * 
     * @param job
     * @throws TspException 
     */
    @Override
    public void deleteJobDetail(Job job) throws TspException {
        try {
            TspJobDetail tspJobDetailVO = new TspJobDetail();
            tspJobDetailVO.setJobId(job.getId());
            tspJobDetailVO.setClusterName(job.getClusterName());
            
            TspClient tspClient = getClient(job.getClusterName());
            TspResult<?> tspResult = tspClient.deleteJobDetail(tspJobDetailVO);
            validateResult(tspResult);
        } catch (Exception e) {
            throw new TspException(e.getMessage());
        }
    }
    
    @Override
    public Job get(Job job) throws TspException {
        try {
            TspJob tspJobVO = new TspJob();
            BeanUtils.applyIf(tspJobVO, job, false);
            
            TspClient tspClient = getClient(job.getClusterName());
            TspResult<TspJob> tspResult = tspClient.queryJob(tspJobVO);
            validateResult(tspResult);
            
            Job rjob = new Job();
            BeanUtils.applyIf(rjob, tspResult.getRetVo().getObject(), false);
            return rjob;
        } catch (Exception e) {
            throw new TspException(e.getMessage());
        }
    }
    

    @Override
    public void pauseJob(Job job) throws TspException {
        try {
            TspJob tspJobVO = new TspJob();
            BeanUtils.applyIf(tspJobVO, job, false);
            
            TspClient tspClient = getClient(job.getClusterName());
            TspResult<?> tspResult = tspClient.pauseJob(tspJobVO);
            validateResult(tspResult);
        } catch (Exception e) {
            throw new TspException(e.getMessage());
        }
    }
    
    @Override
    public void pauseJob(List<Job> jobList) throws TspException {
        for(Job job : jobList){
            pauseJob(job);
        }
    }

    @Override
    public List<Job> query(Job job) throws TspException {
        try {
            TspJob tspJobVO = new TspJob();
            BeanUtils.applyIf(tspJobVO, job, false);
            
            TspClient tspClient = getClient(job.getClusterName());
            TspResult<TspJob> tspResult = tspClient.queryJob(tspJobVO);
            validateResult(tspResult);
            
            List<TspJob> tspJobVOList = new ArrayList<TspJob>();
            tspJobVOList.addAll(tspResult.getRetVo().getDataList());
            return BeanUtils.copyList(tspJobVOList, Job.class);
        } catch (Exception e) {
            throw new TspException(e.getMessage());
        }
    }
    
    @Override
    public PageInfo<Job> queryPage(Job job, int pageNumber, int pageSize) throws TspException {
        try {
            TspJob tspJobVO = new TspJob();
            BeanUtils.applyIf(tspJobVO, job, false);
            tspJobVO.setPageNumber(pageNumber);
            tspJobVO.setPageSize(pageSize);
            
            TspClient tspClient = getClient(job.getClusterName());
            TspResult<TspJob> tspResult = tspClient.queryJob(tspJobVO);
            validateResult(tspResult);
            
            com.ffcs.crmd.tsp.api.dto.page.PageInfo<TspJob> tspJobGroupPageInfo = tspResult.getRetVo().getPageInfo();
            PageInfo<Job> jobGroupPageInfo = new PageInfo<Job>();
            BeanUtils.applyIf(jobGroupPageInfo, tspJobGroupPageInfo, false);
            jobGroupPageInfo.setList(BeanUtils.copyList(tspJobGroupPageInfo.getList(), Job.class));
            return jobGroupPageInfo;
        } catch (Exception e) {
            throw new TspException(e.getMessage());
        }
    }
    
    
    @Override
    public void resumeJob(Job job) throws TspException {
        try {
            TspJob tspJobVO = new TspJob();
            BeanUtils.applyIf(tspJobVO, job);
            TspClient tspClient = tspClientService.getClient(job.getClusterName());
            TspResult<?> tspResult = tspClient.resumeJob(tspJobVO);
            validateResult(tspResult);
        } catch (Exception e) {
            throw new TspException(e.getMessage());
        }
    }

    @Override
    public void resumeJob(List<Job> jobList) throws TspException {
        for(Job job : jobList){
            resumeJob(job);
        }
    }
    
    
    @Override
    public void saveJob(Job job) throws TspException {
        try {
            TspJob tspJobGroupVO = new TspJob();
            BeanUtils.applyIf(tspJobGroupVO, job);
            TspClient tspClient = tspClientService.getClient(job.getClusterName());
            TspResult<?> tspResult = tspClient.createJob(tspJobGroupVO);
            validateResult(tspResult);
        } catch (Exception e) {
            throw new TspException(e.getMessage());
        }
    }
    
        
    @Override
    public void updateJob(Job job) throws TspException {
        try {
            TspJob tspJobGroupVO = new TspJob();
            BeanUtils.applyIf(tspJobGroupVO, job);
            TspClient tspClient = tspClientService.getClient(job.getClusterName());
            TspResult<?> tspResult = tspClient.updateJob(tspJobGroupVO);
            validateResult(tspResult);
        } catch (Exception e) {
            throw new TspException(e.getMessage());
        }
    }
    
}
